package com.haier.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haier.dto.HpGardenDto;
import com.haier.entity.HpGarden;
import com.haier.mapper.HpAreaAuditorMapper;
import com.haier.mapper.HpAreaFreeTimeMapper;
import com.haier.mapper.HpAreaMapper;
import com.haier.mapper.HpGardenMapper;
import com.haier.service.IHpGardenService;
import com.haier.utils.BeanUtil;
import com.haier.utils.EmptyUtils;
import com.haier.utils.GetUtils;
import com.haier.vo.ResponseVo;

/**
 * <p>
 * 园区 服务实现类
 * </p>
 *
 * @author luanxuecheng
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class HpGardenServiceImpl extends ServiceImpl<HpGardenMapper, HpGarden> implements IHpGardenService {
	@Autowired
	private HpAreaMapper hpAreaMapper;
	@Autowired
	private HpAreaAuditorMapper hpAreaAuditorMapper;
	@Autowired
	private HpAreaFreeTimeMapper hpAreaFreeTimeMapper;

	@Override
	public ResponseVo tree(HpGardenDto dto) {
		Boolean onlyArea = dto.getOnlyArea();
		if (onlyArea == null) {
			onlyArea = false;
		}
		List<Map<String, Object>> gardenList = baseMapper.tree();
		if (!EmptyUtils.isEmpty(gardenList)) {
			for (int i = 0; i < gardenList.size(); i++) {
				Map<String, Object> garden = gardenList.get(i);
				Long gardenId = GetUtils.getLong(garden.get("id"));
				List<Map<String, Object>> areaList = hpAreaMapper.tree(gardenId);
				if (!EmptyUtils.isEmpty(areaList)) {
					if (!onlyArea) {
						for (int j = 0; j < areaList.size(); j++) {
							Map<String, Object> area = areaList.get(j);
							Long areaId = GetUtils.getLong(area.get("id"));
							List<Map<String, Object>> areaAuditorList = hpAreaAuditorMapper.tree(areaId);
							area.put("areaAuditorList", areaAuditorList);
							List<Map<String, Object>> areaFreeTimeList = hpAreaFreeTimeMapper.tree(areaId);
							area.put("areaFreeTimeList", areaFreeTimeList);
						}
					}
				}
				garden.put("areaList", areaList);
			}
		}
		return ResponseVo.success(gardenList);
	}

	@Override
	public ResponseVo edit(HpGardenDto dto) {
		HpGarden bean = new HpGarden();
		BeanUtil.copyNotNullBean(dto, bean);
		if (EmptyUtils.isEmpty(dto.getId())) {
			baseMapper.insert(bean);
		} else {
			baseMapper.updateById(bean);
		}
		JSONObject result = new JSONObject();
		result.put("id", bean.getId());
		return ResponseVo.success(result);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public ResponseVo remove(HpGardenDto dto) {
		Long id = dto.getId();
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("garden_id", id);
		queryWrapper.eq("is_del", false);
		Integer count = hpAreaMapper.selectCount(queryWrapper);
		if (count > 0) {
			return ResponseVo.checkError("该园区有区域不能删除");
		}
		HpGarden bean = baseMapper.selectById(id);
		if (EmptyUtils.isEmpty(bean)) {
			return ResponseVo.checkError("id无效");
		}
		bean.setIsDel(true);
		baseMapper.updateById(bean);
		return ResponseVo.success();
	}

}
